// components/common/upload/index.js
Component({
    properties: {
        imageList: {
            type: Array
        },
        mediaType: {
            type: String,
            value: 'image'
        },
        count: {
            type: String,
            value: 9
        },
        size: {
            type: String,
            value: "10240000"
        }
    },
    data: {

    },
    methods: {
        chooseFile: function () {
            const { mediaType } = this.data
            switch (mediaType) {
                case 'image':
                    this.addImage();
                    break;
                case 'video':
                    this.addVideo();
                    break;
                default:
                    // ... 
                    break;
            }
        },
        /**
         * 选择图片
         */
        addImage: function () {
            let { count, imageList } = this.data
            let oldCount = imageList.length
            count = count - oldCount
            let that = this
            wx.chooseImage({
                count: count,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: function (res) {
                    wx.showLoading({
                      title: '上传图片中',
                    })
                    const { tempFiles } = res
                    // 新建一个临时数组储存已上传图片
                    let tempImgList = imageList
                    // 建立Promise通道任务
                    const imgUploadTask = tempFiles.map(item => that.uploadImagePromise(item));
                    // 聚合多个 Promise
                    Promise.all(imgUploadTask)
                        .then((tempFileIDs) => {
                            // console.log(tempFileIDs)
                            const imgInfoTask = tempFileIDs.map(item => that.getImagePromise(item.fileID));
                            Promise.all(imgInfoTask)
                                .then((infos)=>{
                                    // console.log(infos)
                                    tempFileIDs.forEach((item, index) => {
                                        tempImgList.push({
                                            url: item.fileID,
                                            size: tempFiles[index].size,
                                            width: infos[index].width,
                                            height: infos[index].height,
                                            type: infos[index].type,
                                        })
                                    })
                                    // console.log(tempImgList)
                                    that.triggerEvent("success", tempImgList)
                                    setTimeout(()=>{
                                        wx.hideLoading()
                                    },100)
                                })
                        })
                },
                fail: function () {},
                complete: function () {}
            })
        },
        getImagePromise: async function (image) {
            return await wx.getImageInfo({
                src: image
            })
        },
        uploadImagePromise: function (image) {
            // console.log(image)
            let time = new Date();
            // let savePath = "images/" + time.getFullYear() + "-" + time.getMonth()+1 + "/";
            let fileName = image.path.substr(11);
            // return "images/" + time.getFullYear() + "/" + time.getMonth()+1 + "/" + fileName;
            return wx.cloud.uploadFile({
                cloudPath: "images/" + time.getFullYear() + "/" + time.getMonth()+1 + "/" + fileName,
                filePath: image.path
            })
        }
    }
});